Skip to content

feat: context optimization — AST skeleton caching for Read tool token reduction#18

Merged
bhvbhushan merged 5 commits into
mainfrom
bhvbhushan/context-optimization
Apr 7, 2026
Merged

feat: context optimization — AST skeleton caching for Read tool token reduction#18
bhvbhushan merged 5 commits into
mainfrom
bhvbhushan/context-optimization

Conversation

@bhvbhushan

Copy link
Copy Markdown
Owner

Summary

v0.4.1 — Context Optimization. Intercepts Claude Code Read tool re-reads to reduce token consumption via AST skeleton caching.

  • AST utils refactor — new src/ast-utils.ts with shared findImports(), findFunctions(), findClasses(), findExports(). 4 detectors refactored, 68 lines of duplication removed.
  • Skeleton extraction — compresses source files to structural summaries (imports + function signatures + class outlines). 91% reduction on engine.ts (684 lines → 49).
  • SQLite cachebun:sqlite with WAL mode. Tracks skeletons, session reads, and token savings per session.
  • Hook handlers — PreToolUse smart-limits unchanged re-reads to 30 lines + skeleton. PostToolUse caches skeleton after each read. PostCompact prunes old sessions.
  • vibecop init --context — generates Claude Code hooks with conflict detection. Resolves absolute paths for both local dev and npm installs.
  • vibecop context benchmark — scans project, shows per-file compression ratios and projected savings. Runs under node (no bun required).
  • MCP tool: vibecop_context_benchmark — same benchmark accessible through MCP server.
  • Documentation — README, CHANGELOG, CONTRIBUTING, Starlight docs (architecture, MCP, Claude Code, CLI reference) all updated.

Test Coverage

610 tests pass (42 new). Type check clean. Lint clean. Both builds succeed.

New tests:
  test/ast-utils.test.ts           — 28 tests (findImports, findFunctions, findClasses, findExports)
  test/context/skeleton.test.ts    — 12 tests (extraction quality, edge cases)
  test/context/cache.test.ts       — 15 tests (SQLite ops, WAL mode, pruning)
  test/context/integration.test.ts — 11 tests (end-to-end hook protocol)
  test/init.test.ts                — 4 new tests (--context flag, conflict detection)

Validation Results

22/22 validation checks pass (bun scripts/validate-context.ts):

  • Skeleton quality: 91% reduction on engine.ts, ~4,446 tokens saved per re-read
  • Latency: P90 pre-hook 21ms (budget: <200ms)
  • Protocol: all edge cases pass through cleanly (invalid JSON, missing session_id, unsupported files, partial reads, changed files)

Benchmark (on vibecop itself)

Files:        147 supported
Total tokens: ~171,448

Projected savings per session:
  20% re-read rate:  ~56,117 tokens saved (33% of total Read usage)
  40% re-read rate:  ~85,810 tokens saved (50% of total Read usage)
  60% re-read rate:  ~106,543 tokens saved (62% of total Read usage)

Architecture

Build Runtime Contents
dist/cli.js (1.32MB) node CLI + MCP + benchmark (no bun:sqlite)
dist/context.js (26KB) bun Hook handlers + stats (bun:sqlite for caching)

Test plan

  • 610 tests pass (bun test)
  • Type check clean (tsc --noEmit)
  • Lint clean (biome check)
  • CLI build succeeds (dist/cli.js)
  • Context build succeeds (dist/context.js)
  • Benchmark runs under pure node (node dist/cli.js context benchmark)
  • Stats runs under bun (bun dist/context.js stats)
  • MCP tool handler returns structured JSON
  • Zero bun:sqlite references in dist/cli.js

🤖 Generated with Claude Code

bhvbhushan and others added 5 commits April 7, 2026 09:42
New src/ast-utils.ts with findImports, findFunctions, findClasses,
findExports, and extractJsPackageName. Refactored 4 detectors
(god-component, god-function, mixed-concerns, undeclared-import)
to use shared utils. Removed 68 lines of duplicated helpers from
god-function.ts. All 568 existing detector tests pass unchanged.
Lane A: AST skeleton extraction (src/context/skeleton.ts) using shared
ast-utils. Compresses 600-line files to ~50-line structural summaries.

Lane B: bun:sqlite cache layer (src/context/cache.ts) with WAL mode.
Tracks skeletons, session reads, and token savings stats.

Benchmark command (src/context/benchmark.ts) runs under node with zero
bun:sqlite dependency — projects savings without requiring bun.

38 new tests across skeleton, cache, and integration suites.
src/context.ts: PreToolUse/PostToolUse/PostCompact hook entry point.
Smart-limits re-reads to 30 lines + skeleton via additionalContext.

src/init.ts: --context flag generates Claude Code hooks with conflict
detection. Resolves absolute paths to dist/context.js for local dev
and npm installs.

src/mcp/server.ts: vibecop_context_benchmark MCP tool — runs under
node, no bun required. Projects token savings for any project.

src/cli.ts: vibecop context [benchmark|stats] command. Benchmark
runs in-process under node. Stats shells out to bun for SQLite.
scripts/validate-context.ts: 22-check validation suite (skeleton
quality, latency benchmarks, end-to-end hook protocol).
scripts/baseline-read-counter.ts: baseline Read counter for A/B testing.
scripts/test-hook-dedup.ts: empirical hook/dedup test harness.
Version 0.4.0 → 0.4.1. CHANGELOG with full v0.4.1 entry.
README: context optimization section, MCP tool count 3→4, Phase 4 done.
CONTRIBUTING: updated project structure, build:context in PR checklist.
Starlight docs: architecture, MCP server, Claude Code, CLI reference.
.gitignore: added .vibecop/ for SQLite cache.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bhvbhushan bhvbhushan merged commit d2b662b into main Apr 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant